import { Body, Inject, Post, Provide } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { ProjectEntity } from '../../entity/project';
import { ProjectService } from '../../service/project';

/**
 * 项目管理
 */
@Provide()
@CoolController({
    api: ['add', 'delete', 'update', 'info', 'list', 'page'],
    entity: ProjectEntity,
    service: ProjectService,
    pageQueryOp: {
        keyWordLikeFields: ['name', 'description'],
        fieldEq: ['status'],
        select: ['a.*', 'b.roleType as userRole'],
        join: [
            {
                entity: 'project_member',
                alias: 'b',
                condition: 'a.id = b.projectId'
            }
        ],
        where: async (ctx) => {
            return [
                [
                    '(b.userId = :userId)',
                    { userId: ctx.user.id }
                ]
            ];
        },
        addOrderBy: {
            createTime: 'DESC'
        }
    }
})
export class ProjectController extends BaseController {
    @Inject()
    projectService: ProjectService;

    /**
     * 更新项目状态
     */
    @Post('/status')
    async status(@Body() body: any) {
        const { id, status } = body;
        await this.projectService.update({
            id,
            status
        });
        return this.ok();
    }
} 